package com.palrevo.utils;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.el.ValueExpression;
import javax.faces.application.Application;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.palrevo.beackBeans.AppSessionBean;
import com.palrevo.beackBeans.ApplicationBean;
import com.palrevo.common.Constants;

/**
 * 
 * @author AAsha
 */
public class WebUtils {
	
	public static final String APP_BEAN = "#{appBean}";
	public static final String APP_SESSION_BEAN = "#{appSessionBean}";
	
	private static final String RESOURCES = "/resources/";
	private static final int BUFSIZE = 4096;
	
	
	public static String getResourcesDir() {

		String resources = ((ServletContext) FacesContext.getCurrentInstance()
				.getExternalContext().getContext()).getRealPath(RESOURCES);

		return resources;

	}

	public static Object resolveExpression(String expression,
			Class<?> objectClass) {
		Application app = FacesContext.getCurrentInstance().getApplication();
		ExpressionFactory elFactory = app.getExpressionFactory();
		ELContext elContext = FacesContext.getCurrentInstance().getELContext();
		ValueExpression valueExp = elFactory.createValueExpression(elContext,
				expression, objectClass);
		return valueExp.getValue(elContext);
	}

	public static ApplicationBean getApplicationBean() {

		Object appBean = resolveExpression(APP_BEAN, ApplicationBean.class);

		if (appBean == null) {
			return null;
		}

		return (ApplicationBean) appBean;
	}

	public static AppSessionBean getAppSessionBean() {

		Object sessionBean = resolveExpression(APP_SESSION_BEAN,
				AppSessionBean.class);

		if (sessionBean == null) {
			return null;
		}

		return (AppSessionBean) sessionBean;
	}

	public static HttpSession getHttpSession(boolean forceCreate) {
		return (HttpSession) FacesContext.getCurrentInstance()
				.getExternalContext().getSession(forceCreate);
	}

	public static HttpServletRequest getRequest() {
		return (HttpServletRequest) FacesContext.getCurrentInstance()
				.getExternalContext().getRequest();
	}

	public static void setSessionAttribute(String name, Object value) {
		HttpSession session = WebUtils.getHttpSession(false);
		synchronized (session) {
			session.setAttribute(name, value);
		}
	}

	public static void removeSessionAttribute(String name) {
		HttpSession session = WebUtils.getHttpSession(false);
		synchronized (session) {
			session.removeAttribute(name);
		}
	}

	public static Object getSessionAttribute(String name) {
		HttpSession session = WebUtils.getHttpSession(false);
		if (session != null) {
			synchronized (session) {
				return session.getAttribute(name);
			}
		} else {
			return null;
		}

	}

	/**
	 * 
	 * @return View's loacle.
	 */
	public static Locale getCurrentLocal() {
		return FacesContext.getCurrentInstance().getViewRoot().getLocale();
	}

	/**
	 * 
	 * @return ResourceBundle
	 */
	public static ResourceBundle getResourceBundle() {
		return getResourceBundle(getCurrentLocal());
	}

	/**
	 * 
	 * @param locale
	 * @return Application Resource Bundle.
	 */
	public static ResourceBundle getResourceBundle(Locale locale) {
		return ResourceBundle.getBundle(Constants.BUNDLE_BASENAME, locale);
	}

	/**
	 * 
	 * @param file
	 * @throws IOException
	 */
	public static void downloadFile(File file) throws IOException {

		FacesContext fc = FacesContext.getCurrentInstance();

		ServletContext servletContext = (ServletContext) fc
				.getExternalContext().getContext();
		HttpServletResponse response = (HttpServletResponse) fc
				.getExternalContext().getResponse();

		String fileName = file.getName();
		int fileLength = (int) file.length();
		String filePath = file.getPath();

		response.reset();
		response.setContentType(servletContext.getMimeType(filePath));
		response.setContentLength(fileLength);
		response.setHeader("Content-Disposition", "attachment; filename=\""
				+ fileName + "\"");

		OutputStream outStream = response.getOutputStream();

		byte[] byteBuffer = new byte[BUFSIZE];
		DataInputStream in = new DataInputStream(new FileInputStream(file));

		// reads the file's bytes and writes them to the response stream
		while ((in != null) && ((fileLength = in.read(byteBuffer)) != -1)) {
			outStream.write(byteBuffer, 0, fileLength);
		}

		in.close();
		outStream.close();

		fc.responseComplete();
	}

}
